<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel frontend\models\CartSeacher */
/* @var $dataProvider yii\data\ActiveDataProvider */

//$this->title = 'Carts';
//$this->params['breadcrumbs'][] = $this->title;
$cartTotal = $this->params['cartTotal'];
$cartList = $this->params['cartList'];
?>
<section id="cart-page">
    <div class="col-xs-12 col-md-9 items-holder no-margin">
		<form id="cartList" action="<?php echo Url::toRoute('shop-order/create'); ?>" method="post">
			<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
			<?php
			foreach ($cartList as $cartUnit) {
				?>
				<!-- /.cart-item -->  
				<div class="row no-margin cart-item" id = "product<?php echo $cartUnit['productId'] ?>">
					<div class="col-xs-12 col-sm-1 no-margin" style="padding-top: 50px;">
						<input  class=" big qty" type="checkbox"  name="cartIdC[]" value="<?php echo $cartUnit['cartId']; ?>"/>
					</div>
					<div class="col-xs-12 col-sm-2 no-margin">
						<a href="#" class="thumb-holder">
							<img class="lazy" alt="" src="//<?php echo $cartUnit['productCover'] ?>" />
						</a>
					</div>

					<div class="col-xs-12 col-sm-4 ">
						<div class="title">
							<a href="<?php echo \yii\helpers\Url::toRoute(['product/view', 'id' => $cartUnit['productId']]) ?>"><?php echo $cartUnit['productTitle'] ?></a>
						</div>
						<div class="brand">特卖</div>
					</div> 

					<div class="col-xs-12 col-sm-3 no-margin">
						<div class="quantity">
							<div class="le-quantity">
								<a class="minus" href="#product<?php echo $cartUnit['productId'] ?>" 
								   onclick="minusProduct(<?php echo $cartUnit['productId'] ?>,<?php echo $cartUnit['price'] ?>)"></a>
								<input name="quantity" id="productNumber<?php echo $cartUnit['productId'] ?>" 
									   readonly="readonly" type="text" value="<?php echo $cartUnit['productnum'] ?>" />
								<a class="plus" href="#product<?php echo $cartUnit['productId'] ?>"
								   onclick="plusProduct(<?php echo $cartUnit['productId'] ?>,<?php echo $cartUnit['price'] ?>)"></a>
							</div>
						</div>
					</div> 
					<div class="col-xs-12 col-sm-2 no-margin">
						<div class="price">
							<p id = "productTotal<?php echo $cartUnit['productId'] ?>"> <?php echo $cartUnit['productTotal'] ?></p>
						</div>
						<button class="close-btn" 
								onclick="doDelete(<?php echo $cartUnit['productId'] ?>,
								<?php echo $cartUnit['productnum'] ?>,
								<?php echo $cartUnit['price'] ?>
										)"></button>
					</div>
				</div><!-- /.cart-item END-->   
			<?php } ?>
		</form>
    </div>
    <!-- ========================================= SIDEBAR ========================================= -->

    <div class="col-xs-12 col-md-3 no-margin sidebar ">
        <div class="widget cart-summary">
            <h1 class="border">购物车</h1>
            <div class="body">
                <ul class="tabled-data no-border inverse-bold">
                    <li>
                        <label>购物车总计</label>
                        <div class="value pull-right" id="cartTotal1"><?php echo $cartTotal ?></div>
                    </li>
                    <li>
                        <label>运费</label>
                        <div class="value pull-right">包邮</div>
                    </li>
                </ul>
                <ul id="total-price" class="tabled-data inverse-bold no-border">
                    <li>
                        <label>订单总计</label>
                        <div class="value pull-right" id="cartTotal2"><?php echo $cartTotal ?></div>
                    </li>
                </ul>
                <div class="buttons-holder">
                    <a class="le-button big" href="#" onclick="sendCartList()">结算</a>
                    <a class="simple-link block" href="<?php Url::toRoute('site/index') ?>" >继续购物</a>
                </div>
            </div>
        </div><!-- /.widget -->

        <div id="cupon-widget" class="widget">
            <h1 class="border">使用优惠卷</h1>
            <div class="body">
                <form>
                    <div class="inline-input">
                        <input data-placeholder="请输入优惠卷" type="text" />
                        <button class="le-button" type="submit">提交</button>
                    </div>
                </form>
            </div>
        </div><!-- /.widget -->
    </div><!-- /.sidebar -->

    <!-- ========================================= SIDEBAR : END ========================================= -->

</section>
<script type="text/javascript">

	function  minusProduct(productId, salePrice) {
//        alert("ss");
		var productNumber = document.getElementById("productNumber" + productId);

		if (productNumber.value <= 1) {
			productNumber.value = 1;
		} else {
			var csrfToken = "<?php echo Yii::$app->request->csrfToken; ?>";
			$.ajax({
				type: "POST",
				url: '<?php echo Yii::$app->request->baseUrl . '/cart/minus'; ?>',
				data: {id: productId, number: 1, _csrf: csrfToken},
				success: function (data) {
//                alert("添加购物车成功");
					if (productTotal !== null) {
						var productTotal = document.getElementById("productTotal" + productId);
						productTotal.innerHTML = parseInt(productTotal.innerHTML) - salePrice;
						var cartTotal1 = document.getElementById("cartTotal1");
						cartTotal1.innerHTML = parseInt(cartTotal1.innerHTML) - salePrice;
						var cartTotal2 = document.getElementById("cartTotal2");
						cartTotal2.innerHTML = parseInt(cartTotal2.innerHTML) - salePrice;
						console.log(data);
					}
				},
				error: function (data) {
					alert("失败");
					console.log(data);
				},
			});
		}
	}
	function  plusProduct(productId, salePrice) {
		var csrfToken = "<?php echo Yii::$app->request->csrfToken; ?>";
		$.ajax({
			type: "POST",
			url: '<?php echo Yii::$app->request->baseUrl . '/cart/add'; ?>',
			data: {id: productId, number: 1, _csrf: csrfToken},
			success: function (data) {
//                alert("添加购物车成功");
				if (productTotal !== null) {
					var productTotal = document.getElementById("productTotal" + productId);
					productTotal.innerHTML = parseInt(productTotal.innerHTML) + salePrice;
					var cartTotal1 = document.getElementById("cartTotal1");
					cartTotal1.innerHTML = parseInt(cartTotal1.innerHTML) + salePrice;
					var cartTotal2 = document.getElementById("cartTotal2");
					cartTotal2.innerHTML = parseInt(cartTotal2.innerHTML) + salePrice;
					console.log(data);
				}
			},
			error: function (data) {
				alert("添加失败");
				console.log(data);
			},
		});
	}


	function sendCartList() {
//		alert('send');
		var iForm = document.getElementById('cartList');
		iForm.submit();
	}


//    minusProduct() plusProduct() deleteProduct()
</script>
